<template>
  <div class="login_container">
    <div class="login_box" v-loading.fullscreen="loading">
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>

      <el-form
        ref="loginform"
        :model="loginform"
        :rules="rules"
        label-width="0px"
        class="login_form"
      >
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user"
            v-model="loginform.username"
          ></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            show-password
            prefix-icon="el-icon-alarm-clock"
            v-model="loginform.password"
          >
          </el-input>
        </el-form-item>

        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// const qs = require('qs');
export default {
  name: "login",
  data() {
    return {
      loading: false,
      loginform: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入登陆密码",
            trigger: "change",
          },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    open1() {
      const h = this.$createElement;

      this.$notify({
        title: "标题名称",
        message: h(
          "i",
          {
            style: "color: teal",
          },
          "请输入正确的用户名和密码"
        ),
      });
    },
    open2(string) {
      this.$message({
        message: "这是一条" + string + "消息",
        type: "success",
      });
    },
    reset() {
      this.$refs["loginform"].resetFields();
    },
    login() {
      this.$refs["loginform"].validate(async (valie) => {
        if (valie) {
          this.loading = true;
          const reselt = await this.$axios.post(
            "http://timemeetyou.com:8889/api/private/v1/login",
            this.loginform
          );
          this.loading = false;
          if (reselt.data.meta.msg == "登录成功") {
            this.open2("成功");
            window.sessionStorage.setItem("token", reselt.data.data.token);

            this.$router.push("/home");
          } else {
            this.$message.error("错了哦，这是一条错误消息");
          }
        } else {
          console.log("失败。。。。");
          this.open1();
          return;
        }
      });
    },
  },
};
</script>

<style  scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 10px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.avatar_box {
  width: 130px;
  height: 130px;
  padding: 10px;
  border: 1px solid #eee;
  border-radius: 50%;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
}

.avatar_box img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}

.btns {
  display: flex;
  justify-content: flex-end;
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 25px;
  box-sizing: border-box;
}
</style>
